Fx.Height = Fx.Style.extend({initialize: function(el, options){$(el).setStyle('overflow', 'hidden');this.parent(el, 'height', options);},toggle: function(){var style = this.element.getStyle('height').toInt();return (style > 0) ? this.start(style, 0) : this.start(0, this.element.scrollHeight);},show: function(){return this.set(this.element.scrollHeight);}});Fx.Opacity = Fx.Style.extend({initialize: function(el, options){this.now = 1;this.parent(el, 'opacity', options);},toggle: function(){return (this.now > 0) ? this.start(1, 0) : this.start(0, 1);},show: function(){return this.set(1);}});

window.addEvent("load",function(){
	
	$$(".gk_news_image_5_wrapper").each(function(el){
		// generowanie rdzenia
		var mainwrap = el;
		var elID = el.getProperty("id");
		var $G = $Gavick[elID]; 
		var wrap = $(elID);
		
		$G["actual_slide"] = -1;
		$G["actual_anim"] = false;
		$G["actual_anim_p"] = false;
		
		var slides = [];
		var contents = [];
		var pasek = false;
		
		var jsonlinks = ($E('.gk_news_image_5_json_links')) ? $E('.gk_news_image_5_json_links').getChildren() : false;
		
		if(window.ie){
			if($E(".gk_news_image_5_text_bg", wrap)) $E(".gk_news_image_5_text_bg",wrap).setOpacity($G["opacity"].toFloat());
		}
		
		wrap.getElementsBySelector(".gk_news_image_5_slide").each(function(elmt,i){
			if(jsonlinks){
				elmt.addEvent("click", function(){
					window.location = jsonlinks[i].getProperty("href");	
				});	
				
				elmt.setStyle("cursor", "pointer");
			}
			
			slides[i] = elmt;
		});
		
		slides.each(function(el,i){if(i != 0) el.setOpacity(0);});
		
		if($E(".gk_news_image_5_text_bg",wrap)){
			var text_block = $E(".gk_news_image_5_text_bg",wrap);
			$ES(".gk_news_image_5_news_text",wrap).each(function(el,i){contents[i] = el.innerHTML;});
		}
		
		// animacje
		var amount_c = contents.length-1;
		if($E(".gk_news_image_5_text",wrap)) $E(".gk_news_image_5_text",wrap).innerHTML = contents[0];
		
		var NI2 = new news_image_5();
		
		$ES(".gk_news_image_5_tab",mainwrap).each(function(elx,index){
			var scrollo = new Fx.Scroll($E('.gk_news_image_5_tabsbar_wrap', wrap, {duration: 250}));
			
			elx.addEvent("click",function(){
				if(!$G["actual_anim_p"]){
					$E(".gk_news_image_5_tab_active",mainwrap).setProperty("class","gk_news_image_5_tab");
					elx.setProperty("class","gk_news_image_5_tab_active");
					scrollo.toElement(elx);
				}
				
				NI2.image_anim(elID,mainwrap,wrap,slides,index,contents,$G,false);
			});
		});
		
		$E(".gk_news_image_5_tab",mainwrap).setProperty("class","gk_news_image_5_tab_active");
		NI2.image_anim(elID,mainwrap,wrap,slides,0,contents,$G,($G["autoanim"]==1));
		
		var allSize = 0;
		var allCount = 0;
		 
		$ES(".gk_news_image_5_tabsbar li",mainwrap).each(function(el, i){
			allSize += el.getSize().size.x;
			allCount++;
		});
		
		$E(".gk_news_image_5_tabsbar_wrap",mainwrap).setStyle("height", $E(".gk_news_image_5_tabsbar li",mainwrap).getSize().size.y+1+"px");
		$E(".gk_news_image_5_tabsbar",mainwrap).setStyle("width", allSize+((allCount*1) + 1)+"px");
		
		var scroller = new Scroller($E(".gk_news_image_5_tabsbar_wrap", mainwrap), {area: 40, velocity: 2, onChange: function(x){this.element.scrollTo(x, 0);}});
		$E(".gk_news_image_5_tabsbar_wrap",mainwrap).addEvent('mouseover', scroller.start.bind(scroller));
		$E(".gk_news_image_5_tabsbar_wrap",mainwrap).addEvent('mouseout', scroller.stop.bind(scroller));
	});
});

var news_image_5 = new Class({

	text_anim : function(wrap,contents,$G){
		var txt = $E(".gk_news_image_5_text",wrap);
		var bg = $E(".gk_news_image_5_text_bg",wrap);

		if(txt){
			if($G["anim_type_t"] == 0){	
				new Fx.Opacity(txt,{duration: $G["anim_speed"]/2}).start(1,0);
				(function(){
					new Fx.Opacity(txt,{duration: $G["anim_speed"]/2}).start(0,1);txt.innerHTML = contents[$G["actual_slide"]];
						bg.setStyles({
							"bottom": "0px",
							"height": (txt.getSize().size.y)+"px"
						});
						txt.setStyle("bottom", "0px");
				}).delay($G["anim_speed"]);
			}	
			else{ 
				txt.innerHTML = contents[$G["actual_slide"]];
				txt.setStyle("top",(wrap.getSize().size.y - txt.getSize().size.y)+"px");
				bg.setStyles({
					"top": (wrap.getSize().size.y - txt.getSize().size.y) + "px",
					"height": (txt.getSize().size.y)+"px"
				});
			}
		}
	},

	image_anim : function(elID,mainwrap,wrap,slides,n,contents,$G,play){
		var max = slides.length-1;
		var scrollo = new Fx.Scroll($E('.gk_news_image_5_tabsbar_wrap', wrap, {duration: 250}));
		if(!$G["actual_anim_p"] && n != $G["actual_slide"]){
			$G["actual_anim_p"] = true;
			
			var actual_slide = $G["actual_slide"];
			$G["actual_slide"] = n;
			slides[n].setStyle("z-index",max+1);
		
			if(actual_slide != -1) new Fx.Opacity(slides[actual_slide],{duration: $G["anim_speed"]}).start(1,0);
			new Fx.Opacity(slides[n],{duration: $G["anim_speed"]}).start(0,1);
			this.text_anim(wrap,contents,$G);	
				
			switch($G["anim_type"]){
				case 0: break;
				case 1: new Fx.Style(slides[n],'margin-top',{duration: $G["anim_speed"]}).start((-1)*slides[n].getSize().size.y,0);break;
				case 2: new Fx.Style(slides[n],'margin-left',{duration: $G["anim_speed"]}).start((-1)*slides[n].getSize().size.x,0);break;
				case 3: new Fx.Style(slides[n],'margin-top',{duration: $G["anim_speed"]}).start(slides[n].getSize().size.y,0);break;
				case 4: new Fx.Style(slides[n],'margin-left',{duration: $G["anim_speed"]}).start(slides[n].getSize().size.x,0);break;
			}
			
			if(play){
				$E(".gk_news_image_5_tab_active",mainwrap).setProperty("class","gk_news_image_5_tab");
				scrollo.toElement($ES(".gk_news_image_5_tab",mainwrap)[n]);
				$ES(".gk_news_image_5_tab",mainwrap)[n].setProperty("class","gk_news_image_5_tab_active");
			}
		
			(function(){slides[n].setStyle("z-index",n);}).delay($G["anim_speed"]);
			(function(){$G["actual_anim_p"] = false;}).delay($G["anim_speed"]);
			
			var $this = this;
			
			if(!play) this.image_pause($G);
			if((play || $G["autoanim"] == 1) && ($G["actual_anim"] == false)){
				$G["actual_anim"] = (function(){
					n = (n < max) ? n+1 : 0;
					$this.image_anim(elID,mainwrap,wrap,slides,n,contents,$G,true);
				}).periodical($G["anim_speed"]*2+$G["anim_interval"]);
			}
		}
	},

	image_pause : function($G){
		$clear($G["actual_anim"]);
		$G["actual_anim"] = false;
	}
});